﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Stu_sys.Entity;
using Stu_sys.Models;
using Stu_sys.utils;
using System.Text.Json;

namespace Stu_sys.Controllers
{
    public class ClassRoomController : Controller
    {
        private readonly MysqlContext _context;

        public ClassRoomController(MysqlContext mysqlContext)
        { 
            _context = mysqlContext;
        }
        public IActionResult ClassRoomList()
        {
            return View();
        }
        public IActionResult GetClassRoomList()
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(ClassRoomViewModel));
            var classRoomList = _context.ClassRooms.ToList();

            var classList=_context.ClassEntities.ToList();
            var res=from cr in classRoomList
                    join cl in classList on cr.ClassRoomId equals cl.ClassRoomId
                    into cv
                    from cl in cv.DefaultIfEmpty()
                    select new  { 
                        cr.ClassRoomId,
                        cr.RoomName,
                        cr.Position,
                        cl?.ClassName,
                        cr.Capacity,
                        cr.Description,
                        cr.CreateTime, 
                        cr.UpdateTime
                    };


            var classRoomStr = JsonSerializer.Serialize(res);
            var data = new
            {
                attr = attrStr,
                row = classRoomStr
            };
            return Json((data));
        }


        [HttpPost]
        public IActionResult AddClassRoom(ClassRoom classRoom)
        {
            if (classRoom.RoomName==null)
            {
                return Json(-1);
            }
            if (IsExistClassRoom(classRoom)) 
            {
                return Json(0);
            }
            _context.Add<ClassRoom>(classRoom);
            var res=_context.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult EditClassRoom(ClassRoom classRoom)
        {

            if (classRoom.RoomName == null || classRoom.Capacity < 0)
            {
                return Json(-1);
            }

            var cr = _context.ClassRooms.Where(d => d.ClassRoomId == classRoom.ClassRoomId).FirstOrDefault();
            cr.RoomName = classRoom.RoomName;
            cr.Capacity = classRoom.Capacity;
            cr.Position=classRoom.Position;
            cr.Description = classRoom.Description;

            _context.ClassRooms.Update(cr);
            var res = _context.SaveChanges();
            return Json(res);
        }


        [HttpPost]
        public IActionResult DelClassRoom(ClassRoom classRoom)
        {
            if (classRoom == null)
            {
                return Json(-1);
            }
            var d = _context.ClassRooms.Single(d => d.ClassRoomId == classRoom.ClassRoomId);
            _context.Remove(d);
            var res = _context.SaveChanges();
            return Json(res);
        }


        /// <summary>
        /// 教室是否已存在
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        private bool IsExistClassRoom(ClassRoom classRoom)
        {
            var cr = _context.ClassRooms.Where(o => o.RoomName == classRoom.RoomName).ToList();
            if (cr.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
